// const nums = [1, 3, 5, 6], target = 5; // 2
const nums = [1,3,5,6], target = 2 // 输出1
// const nums = [1,3,5], target = 3 // 输出1
// const nums = [1,3,5,6], target = 7
var searchInsert = function (nums, target) {
  let left = 0;
  let right = nums.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    const e = nums[mid];
    debugger
    if (e === target) {
      return mid;
    }
    // 大于中点 移动左边
    if (target > e) {
      left = mid + 1;
    }
    // 小于中点 移动右边
    if (target < e) {
      right = mid - 1;
    }
  }
  return left;
};

console.log(searchInsert(nums, target));